﻿using System;
using System.Collections.Generic;
using System.Transactions;
using Bingosoft.Data;
using System.Data;

namespace Bingosoft.Modules.SecurityConsole.Organization
{
    public class OrganizationService
    {
        private readonly Dao dao = Dao.Get();

        /// <summary>
        /// 启用组织
        /// </summary>
        /// <param name="roleId">组织Id</param>
        public void EnableOrg(string roleId)
        {
            using (TransactionScope transactionScope = new TransactionScope(TransactionScopeOption.Required))
            {
                IDictionary<string, object> parameters = new Dictionary<string, object> { { "orgId", roleId } };
                dao.ExecuteNonQuery("organiztion.enabledbyid", parameters);
                transactionScope.Complete();
            }  
        }

        /// <summary>
        /// 停用组织
        /// </summary>
        /// <param name="roleId">组织Id</param>
        public void DisableOrg(string roleId)
        {
            using (TransactionScope transactionScope = new TransactionScope(TransactionScopeOption.Required))
            {
                IDictionary<string, object> parameters = new Dictionary<string, object> { { "orgId", roleId } };
                dao.ExecuteNonQuery("organiztion.disablebyid", parameters);
                transactionScope.Complete();
            }
        }

        /// <summary>
        /// 获取组织信息
        /// </summary>
        /// <param name="orgId">组织Id</param>
        /// <returns></returns>
        public OrganizationInfo FindOrganizationById(string orgId)
        {
            OrganizationInfo org;
            using (TransactionScope transactionScope = new TransactionScope(TransactionScopeOption.Suppress))
            {
                IDictionary<string, object> parameters = new Dictionary<string, object> { { "Id", orgId } };
                org = dao.QueryEntity<OrganizationInfo>("organization.select", parameters);
                transactionScope.Complete();
            }

            if (!string.IsNullOrEmpty(org.Parent))
            {
                org.ParentName = GetOrgName(org.Parent);
            }

            return org;
        }

        public string GetOrgNameGyId(string orgId)
        {
            OrganizationInfo org = FindOrganizationById(orgId);
            return org.Name;
        }

        public DataSet GetFirstOrganizationProcessList(string orgId)
        {
            DataSet ds = null;
            using (TransactionScope transactionScope = new TransactionScope(TransactionScopeOption.Suppress))
            {
                IDictionary<string, object> parameters = new Dictionary<string, object> { { "orgId", orgId } };
                ds = dao.QueryDataSet("organization.organization_process_role_top1_type", parameters);
                transactionScope.Complete();
            }
            return ds;
        }

        /// <summary>
        /// 删除组织及其下属组织信息
        /// </summary>
        /// <param name="orgId">组织Id</param>
        /// <returns></returns>
        public Boolean DeleteOrganization(string orgId)
        {
            using (TransactionScope transactionScope = new TransactionScope(TransactionScopeOption.Required))
            {
                IDictionary<string, object> parameters = new Dictionary<string, object> { { "orgId", orgId } };
                //dao.ExecuteNonQuery("organization.reomveUserFromOrganizationByOrgId", parameters);
                dao.ExecuteNonQuery("organization.deleteOrganizationAndChildrenByOrgId", parameters);
                transactionScope.Complete();
                return true;
            }
        }

        /// <summary>
        /// 批量增加组织的管理角色
        /// </summary>
        /// <param name="orgId"></param>
        /// <param name="roleIds"></param>
        /// <returns></returns>
        public Boolean AddManageRolesForOrganization(string orgId, string[] roleIds)
        {
            using (TransactionScope transactionScope = new TransactionScope(TransactionScopeOption.Required))
            {
                foreach (String roleId in roleIds)
                {
                    if (!string.IsNullOrEmpty(roleId))
                    {
                        IDictionary<string, object> parameters = new Dictionary<string, object>() { { "OrgId", orgId }, { "RoleId", roleId } };
                        dao.ExecuteNonQuery("organization.addManageRole", parameters);
                    }
                }
                transactionScope.Complete();
                return true;
            }
        }

        /// <summary>
        /// 批量增加组织的流程角色
        /// </summary>
        /// <param name="orgId"></param>
        /// <param name="roleIds"></param>
        /// <returns></returns>
        public Boolean AddProcessRolesForOrganization(string orgId, string[] roleIds)
        {
            using (TransactionScope transactionScope = new TransactionScope(TransactionScopeOption.Required))
            {
                foreach (String roleId in roleIds)
                {
                    if (!string.IsNullOrEmpty(roleId))
                    {
                        Guid Id = System.Guid.NewGuid();
                        IDictionary<string, object> parameters = new Dictionary<string, object>() { { "Id", Id }, { "OrgId", orgId }, { "RoleId", roleId } };
                        dao.ExecuteNonQuery("organization.addProcessRole", parameters);
                    }
                }
                transactionScope.Complete();
                return true;
            }
        }

        /// <summary>
        /// 删除组织的管理角色
        /// </summary>
        /// <param name="orgId"></param>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public bool RemoveManageRoleForOrganization(string orgId, string roleId)
        {
            using (TransactionScope tran = new TransactionScope())
            {
                var parameters = new Dictionary<string, object>() { { "OrgId", orgId }, { "RoleId", roleId } };
                dao.ExecuteNonQuery("organization.removeManageRole", parameters);
                tran.Complete();
                return true;
            }
        }

        /// <summary>
        /// 删除组织的流程角色
        /// </summary>
        /// <param name="orgId"></param>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public bool RemoveProcessRoleForOrganization(string orgId, string roleId)
        {
            using (TransactionScope tran = new TransactionScope())
            {
                var parameters = new Dictionary<string, object>() { { "OrgId", orgId }, { "RoleId", roleId } };
                dao.ExecuteNonQuery("organization.removeProcessRole", parameters);
                tran.Complete();
                return true;
            }
        }

        /// <summary>
        /// 批量删除组织的管理角色
        /// </summary>
        /// <param name="orgId"></param>
        /// <param name="roleIds"></param>
        public void RemoveManageRolesForOrganization(string orgId, string[] roleIds)
        {
            foreach (var id in roleIds)
            {
                RemoveManageRoleForOrganization(orgId, id);
            }
        }

        /// <summary>
        /// 批量删除组织的流程角色
        /// </summary>
        /// <param name="orgId"></param>
        /// <param name="roleIds"></param>
        public void RemoveProcessRolesForOrganization(string orgId, string[] roleIds)
        {
            foreach (var id in roleIds)
            {
                RemoveProcessRoleForOrganization(orgId, id);
            }
        }

        public void AddRoleOrg(string wfRoleId, string orgId)
        {
            using (TransactionScope tran = new TransactionScope())
            {
                var param = new Dictionary<string, object>() { { "WfRoleId", wfRoleId }, { "OrgId", orgId } };
                dao.ExecuteNonQuery("organization.add-role-org", param);
                tran.Complete();
            }
        }

        public void AddRoleUser(string wfRoleId, string userId)
        {
            using (TransactionScope tran = new TransactionScope())
            {
                var param = new Dictionary<string, object>() { { "WfRoleId", wfRoleId }, { "UserId", userId } };
                dao.ExecuteNonQuery("organization.add-role-user", param);
                tran.Complete();
            }
        }

        public bool AddRoleOrgs(string wfRoleId, string[] orgIds)
        {
            foreach (var i in orgIds)
            {
                if (!string.IsNullOrEmpty(i))
                {
                    AddRoleOrg(wfRoleId, i);
                }
            }
            return true;
        }

        public bool AddRoleUsers(string wfRoleId, string[] userIds)
        {
            foreach (var i in userIds)
            {
                if (!string.IsNullOrEmpty(i))
                {
                    AddRoleUser(wfRoleId, i);
                }
            }
            return true;
        }

        public void RemoveRoleOrg(string wfRoleId, string orgId)
        {
            using (TransactionScope tran = new TransactionScope())
            {
                var param = new Dictionary<string, object>() { { "WfRoleId", wfRoleId }, { "OrgId", orgId } };
                dao.ExecuteNonQuery("organization.remove-role-org", param);
                tran.Complete();
            }
        }

        public void RemoveRoleUser(string wfRoleId, string userId)
        {
            using (TransactionScope tran = new TransactionScope())
            {
                var param = new Dictionary<string, object>() { { "WfRoleId", wfRoleId }, { "UserId", userId } };
                dao.ExecuteNonQuery("organization.remove-role-user", param);
                tran.Complete();
            }
        }


        /// <summary>
        /// 更新组织信息
        /// </summary>
        /// <param name="usfOrganization">组织信息</param>
        /// <returns></returns>
        public string SaveOrUpdate(OrganizationInfo usfOrganization)
        {
            using (TransactionScope transactionScope = new TransactionScope(TransactionScopeOption.Required))
            {
                if (string.IsNullOrEmpty(usfOrganization.Id))
                {
                    usfOrganization.Id = Guid.NewGuid().ToString();

                    if (string.IsNullOrEmpty(usfOrganization.Parent))
                    {
                        usfOrganization.Parent = null;
                    }
                    dao.ExecuteNonQuery("organization.insert", usfOrganization);
                }
                else
                {
                    if (string.IsNullOrEmpty(usfOrganization.Parent))
                    {
                        usfOrganization.Parent = null;
                    }
                    dao.ExecuteNonQuery("organization.update", usfOrganization);
                }
                transactionScope.Complete();
                return usfOrganization.Id;
            }
        }

        /*
         * 根据实体Id获取或者创建对应的实体
         */
        public OrganizationInfo GetOrCreateUsfOrganization(string pkId)
        {
            return string.IsNullOrEmpty(pkId) ? new OrganizationInfo() : FindOrganizationById(pkId);
        }

        /*
         * 获取组织名称
         */
        public string GetOrgName(string orgId)
        {
            using (TransactionScope transactionScope = new TransactionScope(TransactionScopeOption.Suppress))
            {
                IDictionary<string, object> parameters = new Dictionary<string, object> { { "orgId", orgId } };
                String orgName = dao.QueryScalar<string>("organization.getOrganizationNameByOrgId", parameters);
                transactionScope.Complete();
                return orgName;
            }
        }

       
    }
}
